package com.ruoyi.project.system.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.project.system.domain.KnowledgeCatalog;
import com.ruoyi.project.system.mapper.KnowledgeCatalogMapper;
import com.ruoyi.project.system.service.IKnowledgeCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 安全常识目录Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@Service
public class KnowledgeCatalogServiceImpl implements IKnowledgeCatalogService
{
    @Autowired
    private KnowledgeCatalogMapper knowledgeCatalogMapper;

    /**
     * 查询安全常识目录
     * 
     * @param id 安全常识目录ID
     * @return 安全常识目录
     */
    @Override
    public KnowledgeCatalog selectKnowledgeCatalogById(Integer id)
    {
        return knowledgeCatalogMapper.selectKnowledgeCatalogById(id);
    }

    /**
     * 查询安全常识目录列表
     * 
     * @param knowledgeCatalog 安全常识目录
     * @return 安全常识目录
     */
    @Override
    public List<KnowledgeCatalog> selectKnowledgeCatalogList(KnowledgeCatalog knowledgeCatalog)
    {
        return knowledgeCatalogMapper.selectKnowledgeCatalogList(knowledgeCatalog);
    }

    /**
     * 新增安全常识目录
     * 
     * @param knowledgeCatalog 安全常识目录
     * @return 结果
     */
    @Override
    public int insertKnowledgeCatalog(KnowledgeCatalog knowledgeCatalog)
    {

        KnowledgeCatalog catalog = knowledgeCatalogMapper.selectName(knowledgeCatalog.getName());
        if(catalog!=null){
            throw new CustomException("新增失败,存在相同的类别名称");
        }

        int i = knowledgeCatalogMapper.insertKnowledgeCatalog(knowledgeCatalog);

        if(!knowledgeCatalog.getParentId().toString().equals("0")){
            //获取当前对象的对象id
            KnowledgeCatalog knowCatalog = knowledgeCatalogMapper.selectName(knowledgeCatalog.getName());
            //获取父对象的idtree
            KnowledgeCatalog FaCatalog = knowledgeCatalogMapper.selectKnowledgeCatalogById(knowledgeCatalog.getParentId().intValue());
            //拼接当前对象的树状id
            String idTree = FaCatalog.getIdTree()+","+knowCatalog.getId();
            knowCatalog.setIdTree(idTree);
            knowledgeCatalogMapper.updateKnowledgeCatalog(knowCatalog);
        }else{
            //获取当前对象的对象id
            KnowledgeCatalog knowCatalog = knowledgeCatalogMapper.selectName(knowledgeCatalog.getName());
            //拼接当前对象的树状id
            knowCatalog.setIdTree(knowCatalog.getId()+"");
            knowledgeCatalogMapper.updateKnowledgeCatalog(knowCatalog);
        }
        return i;

    }

    /**
     * 修改安全常识目录
     * 
     * @param knowledgeCatalog 安全常识目录
     * @return 结果
     */
    @Override
    public int updateKnowledgeCatalog(KnowledgeCatalog knowledgeCatalog)
    {

        if(!knowledgeCatalog.getParentId().toString().equals("0")){
            //获取当前对象的对象id
            KnowledgeCatalog knowCatalog = knowledgeCatalogMapper.selectName(knowledgeCatalog.getName());
            //获取父对象的idtree
            KnowledgeCatalog FaCatalog = knowledgeCatalogMapper.selectKnowledgeCatalogById(knowledgeCatalog.getParentId().intValue());
            String idTree = FaCatalog.getIdTree()+","+knowCatalog.getId();
            knowledgeCatalog.setIdTree(idTree);
            return  knowledgeCatalogMapper.updateKnowledgeCatalog(knowledgeCatalog);
        }
        return knowledgeCatalogMapper.updateKnowledgeCatalog(knowledgeCatalog);
    }

    /**
     * 批量删除安全常识目录
     * 
     * @param ids 需要删除的安全常识目录ID
     * @return 结果
     */
    @Override
    public int deleteKnowledgeCatalogByIds(Integer[] ids)
    {
        int i =0;
        for (Integer id : ids) {
            List<Map<String, Object>> maps = knowledgeCatalogMapper.selectCatChildList(id);
            if(maps.size()!=0){
                KnowledgeCatalog knowledgeCatalog = knowledgeCatalogMapper.selectKnowledgeCatalogById(id);
                throw new CustomException("删除失败，当前【"+knowledgeCatalog.getName()+"】节点下含有子节点！");
            }else{
                i = knowledgeCatalogMapper.deleteKnowledgeCatalogByIds(ids);
            }
        }

        return i;
    }

    /**
     * 删除安全常识目录信息
     * 
     * @param id 安全常识目录ID
     * @return 结果
     */
    @Override
    public int deleteKnowledgeCatalogById(Integer id)
    {
        return knowledgeCatalogMapper.deleteKnowledgeCatalogById(id);
    }





}
